<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Order_model', 'order');
		$this->load->model('User_model', 'users');
	}

	public function index()
	{
		
		$data = array(
			//'myCreditList' => $this->order->getOneDayOrder("o.time>'".$day1."' AND o.time<'".$day2."'"),
			'bankList' => $this->users->getBankList(),
			'users'=> $this->users->getRegulars()
		);
		$this->load->view("order", $data);
	}

	//订单审核页面
	public function orderReview(){
		$this->load->view('orderReview');
	}

	//获取所有未审核的订单
	public function noReviewOrder(){
		$page = $this->input->post('page');
		if($page<1){
			$page = 1;
		}
		$show = 10;
		$where = "o.reviewed=0";
		$where1 = "reviewed=0";
		$data = $this->order->getAllOrder($where,$page,$show);
		foreach ($data as $key => $v) {
	    	if($v['regulars'] == 2){
	    		$arr = $this->order->intToStr(array('id'=>$v['consumer']));
	    		$data[$key]['consumer'] = $arr['account'];
	    	}
	    }
	    $data['data'] = $data;
		$data['totalPage'] = ceil($this->order->getAllOrderNum($where1)/$show);
		if($data){
			jechos($data);
		}else{
			jechof("目前没有未审核的订单");
		}
	}
	//得到一个订单所有的操作记录
	public function getOneOrderList(){
		$where = array(
			'bid' => $this->input->post('id'),
			'enable'=>0
		);
		$data = $this->order->getOneOrderList($where);
		if($data){
			jechos($data);
		}else{
			jechof("获取操作记录失败");
		}
	}

	//回滚数据到指定记录
	public function undoData(){
		$where = array(
			'id'=>$this->input->post('id'),
			'enable'=>0
		);
		$data = $this->order->getOneOrderList($where);
		if($this->order->undoData($data)){
			jechos("回滚成功");
		}else{
			jechof("回滚失败");
		}
	}

	//审核通过
	public function approved(){
		$where = array(
			'id'=>$this->input->post('id')
		);
		$data['reviewed'] = 1;
		if($this->order->approved($where,$data)){
			jechos("审核成功");
		}else{
			jechof("审核失败");
		}
	}

	//修改订单内容
	public function editOrderHandle(){
		$data = $this->input->post();
		$where = array(
			'id'=>$this->input->post('id')
		);
		if($this->order->editOrderHandle($where,$data,"修改订单内容")){
			jechos("修改订单成功");
		}else{
			jechof("修改订单失败");
		}
	} 

	//未发货订单页面
	public function delivery(){
		$this->load->view('delivery');
	}

	//已完成订单页面
	public function finished(){
		$this->load->view('finished');
	}

	//得到所有的订单
	public function getAllOrder(){
		$page = $this->input->post('page');
		if($page<1){
			$page = 1;
		}
		$show = 10;
		$day1 = $this->input->post('day1');
		$day2 = $this->input->post('day2');
		$where = "o.enable=0";
		$where1 = "enable=0";
		if(!empty($day2)){
			$where .=" AND o.time<".$day2;
			$where1 .=" AND time<".$day2;
		}

		if(!empty($day1)){
			$where .=" AND o.time>".$day1;
			$where1 .=" AND time>".$day1;
		}
		$data = $this->order->getAllOrder($where,$page,$show);
		foreach ($data as $key => $v) {
	    	if($v['regulars'] == 2){
	    		$arr = $this->order->intToStr(array('id'=>$v['consumer']));
	    		$data[$key]['consumer'] = $arr['account'];
	    	}
	    }
	    $data['data'] = $data;
		$data['totalPage'] = ceil($this->order->getAllOrderNum($where1)/$show);
		if($data){
	    	jechos($data);
	    }else{
	    	jechof("没有未发货的订单");
	    }
	}

	//得到所有未发货的订单
	public function getNodelivery(){
		$page = $this->input->post('page');
		if($page<1){
			$page = 1;
		}
		$show = 10;
		$where = "o.status=0 or o.status=1 AND o.enable=0";
		$where1 = "status=0 or status=1 AND enable=0";
		$data = $this->order->getAllOrder($where,$page,$show);
		foreach ($data as $key => $v) {
	    	if($v['regulars'] == 2){
	    		$arr = $this->order->intToStr(array('id'=>$v['consumer']));
	    		$data[$key]['consumer'] = $arr['account'];
	    	}
	    }
	    $data['data'] = $data;
		$data['totalPage'] = ceil($this->order->getAllOrderNum($where1)/$show);
		if($data){
	    	jechos($data);
	    }else{
	    	jechof("没有未发货的订单");
	    }
	}

	//得到所有已经完成的的订单
	public function getFinished(){
		$page = $this->input->post('page');
		if($page<1){
			$page = 1;
		}
		$show = 10;
		$where = "o.status=3 AND o.enable=0";
		$where1 = "status=3 AND enable=0";
		$data = $this->order->getAllOrder($where,$page,$show);
		foreach ($data as $key => $v) {
	    	if($v['regulars'] == 2){
	    		$arr = $this->order->intToStr(array('id'=>$v['consumer']));
	    		$data[$key]['consumer'] = $arr['account'];
	    	}
	    }
	    $data['data'] = $data;
		$data['totalPage'] = ceil($this->order->getAllOrderNum($where1)/$show);
		if($data){
	    	jechos($data);
	    }else{
	    	jechof("还没有已完成的订单");
	    }
	}

	//得到一天的订单
	public function getOneDayOrder(){
		$page = $this->input->post('page');
		if($page<1){
			$page = 1;
		}
		$show = 10;
		if(time() < (strtotime(date("Y-m-d"))+9*3600)){
			$day1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))-15*3600);
	     	$day2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+9*3600);
		}else if(time() > (strtotime(date("Y-m-d"))+12*3600)){
			$day1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+9*3600);
	     	$day2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+33*3600);
		}
		
	    $data = $this->order->getOneDayOrder("o.time>'".$day1."' AND o.time<'".$day2."' AND o.enable=0",$page,$show);
	    foreach ($data as $key => $v) {
	    	if($v['regulars'] == 2){
	    		$arr = $this->order->intToStr(array('id'=>$v['consumer']));
	    		$data[$key]['consumer'] = $arr['account'];
	    	}
	    }
	    $data['data'] = $data;
	    $data['totalPage'] = ceil($this->order->getOneDayOrderNum("time>'".$day1."' AND time<'".$day2."' AND enable=0")/$show);
	    //var_dump($data);exit;
	    if($data){
	    	jechos($data);
	    }else{
	    	jechof("今日还没有订单!");
	    }
	}

	public function addOrder(){
		$data = $this->input->post();
		preg_match("/^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$/",$data['price'],$m);
	    if(empty($m)){
	      jechof("非法参数");
	    }
		preg_match("/^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$/",$data['number'],$m);
	    if(empty($m)){
	      jechof("非法参数");
	    }
		$data['amount'] = $data['price'] * $data['number'];
		$data['loss'] = 0;
		$data['lossnum'] = 0;
		if($data['checkout'] == 1){
			$data['payment'] = 0;
			$data['status'] = 3;
			$data['actual'] = $data['amount'];
		}else{
			$data['payment'] = 1;
			$data['status'] = 0;
			$data['actual'] = 0;
		}
		if($data['regulars'] == 1){
			if($data['consumer'] == ""){
				$data['consumer'] = 1;
			}
		}else if($data['regulars'] == 2){
			$data['consumer'] = $data['uid'];
		}
		$data['time'] = date("Y-m-d H:i:s",time());
		$data['enable'] = 0;
		$data['operator'] = $_SESSION['id'];
		unset($data['checkout']);
		unset($data['uid']);
		if($this->order->addOrder($data)){
			jechos("添加订单成功");
		}else{
			jechof("添加订单失败");
		}
	}

	//弹出页面
	public function CompLoss(){
		$data['id'] = $this->input->get('id');
		$data['status'] = $this->input->get('status');
		//var_dump($data);exit;
		$this->load->view('CompLoss',$data);
	}

	//更改订单状态
	public function editStatus(){
		$data = $this->input->post();
		$sta = $this->order->getOrderStatus(array('id'=>$data['id']));
		if($data['status'] == 1 && $sta['status'] == 0){
			$da['status'] = 1;
			if($this->order->editOrderStatus(array('id'=>$data['id']),$da,"发货")){
				jechos("发货成功");
			}else{
				jechof("发货失败");
			}
		}else if($data['status'] == 3 && $sta['status'] == 1){
			$da['status'] = 3;
			$da['loss'] = $data['loss'];
			$da['lossnum'] = $data['lossnum'];
			if(!empty($data['memo'])){
				$da['memo'] = $data['memo'];
			}
			if($this->order->editOrderStatus(array('id'=>$data['id']),$da,"订单完成")){
				jechos("订单完成");
			}else{
				jechof("订单确认失败");
			}
		}else if($data['status'] == 4 && $sta['status'] == 0){
			$da['status'] = 4;
			$da['memo'] = $data['memo'];
			if($this->order->editOrderStatus(array('id'=>$data['id']),$da,"取消订单")){
				jechos("订单取消成功");
			}else{
				jechof("订单取消失败");
			}
		}else{
			jechof("订单状态异常，请联系管理员QQ2567134961");
		}
	}

	//得到销售统计数据
	public function getSales(){
		$page = $this->input->post('page');
		if($page<1){
			$page = 1;
		}
		$show = 10;
		$day1 = $this->input->post('day1');
		$day2 = $this->input->post('day2');
		$where = "enable=0";
		$where1 = "enable=0";
		if(!empty($day2)){
			$where .=" AND time<".$day2;
			$where1 .=" AND time<".$day2;
		}

		if(!empty($day1)){
			$where .=" AND time>".$day1;
			$where1 .=" AND time>".$day1;
		}
		$data = array();
		$da = $this->order->getSalesOrder($where,$page,$show);
		foreach ($da as $key => $v) {
			if($v['status']!=4){
				@$data['amount'] += $v['amount'];
				@$data['orderNum'] += floatval($v['number']);
			}
			if($v['status'] == 3){
				@$data['actual'] += $v['actual'];
				@$data['loss'] += $v['loss']; 
				@$data['actualNum'] += floatval($v['number']);
				@$data['finished'] += 1;
			}
			if($v['status'] == 0){
				@$data['delivery'] += 1;
			}
			if($v['status']!=4 || $v['status']!=2){
				@$data['order'] +=1;
			}
			if($v['status'] == 1){
				@$data['oldDelivery'] += 1;
			}
		}
		//var_dump($data);exit;
		if($data){
	    	jechos($data);
	    }else{
	    	jechof("还没有数据");
	    }
	}

	//得到一天种类数量
	public function getAllSpeciesNumOneDay(){
		if(time() < (strtotime(date("Y-m-d"))+9*3600)){
			$day1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))-15*3600);
	     	$day2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+9*3600);
		}else if(time() > (strtotime(date("Y-m-d"))+12*3600)){
			$day1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+9*3600);
	     	$day2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+33*3600);
		}
		$fishType = $this->order->getFish();
		foreach ($fishType as $key => $v) {
			$where = "o.time>'".$day1."' AND o.time<'".$day2."' AND o.enable=0 AND o.status=3 AND o.bandid=".$v['id'];
			$da[] = $this->order->getAllSpeciesNum($where);
			
		}
		$data = array();
		if(!empty($da)){
			foreach ($da as $key => $v) {
				//var_dump($v['num'] == null);
				if($v['num'] != null){
					$data[] = $v;
				}
			}
		}
		if($data){
			jechos($data);
		}else{
			jechof("今天还没有销售的订单的数据");
		}
	}

	//得到一日的销售数据
	public function getSalesOneDay(){
		if(time() < (strtotime(date("Y-m-d"))+9*3600)){
			$day1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))-15*3600);
	     	$day2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+9*3600);
		}else if(time() > (strtotime(date("Y-m-d"))+12*3600)){
			$day1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+9*3600);
	     	$day2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+33*3600);
		}
		$where = "time>'".$day1."' AND time<'".$day2."' AND enable=0";
		$da = $this->order->getSalesOrder($where);
		foreach ($da as $key => $v) {
			if($v['status']!=4){
				@$data['amount'] += $v['amount'];
				@$data['orderNum'] += floatval($v['number']);
			}
			if($v['status'] == 3){
				@$data['actual'] += $v['actual'];
				@$data['loss'] += $v['loss']; 
				@$data['actualNum'] += floatval($v['number']);
				@$data['finished'] += 1;
			}
			if($v['status'] == 0){
				@$data['delivery'] += 1;
			}
			if($v['status']!=4 || $v['status']!=2){
				@$data['order'] +=1;
			}
			if($v['status'] == 1){
				@$data['oldDelivery'] += 1;
			}
		}
		//var_dump($data);exit;
		if($data){
	    	jechos($data);
	    }else{
	    	jechof("还没有数据");
	    }
	}
}

/* End of file Order.php */
/* Location: ./application/controllers/Order.php */